package jtk.project4.fleet.screen.addEditNewEquipment;

import java.awt.FlowLayout;
import java.util.List;

import jtk.project4.fleet.action.CancelAction;
//import jtk.project4.fleet.action.CancelEquipmentAction;
//import jtk.project4.fleet.action.SaveEquipmentAction;
import jtk.project4.fleet.action.SubmitAction;
import jtk.project4.fleet.event.EquipmentEvent;
import jtk.project4.fleet.field.EquipmentInsurancePolicyInformationField;
import jtk.project4.fleet.field.EquipmentNotesField;
import jtk.project4.fleet.field.EquipmentPhotoPhotoField;
import jtk.project4.fleet.field.EquipmentTiresAxlesField;
import jtk.project4.fleet.field.IdentificationField;
import jtk.project4.fleet.field.NewEquipmentCofigureTabField;
import jtk.project4.fleet.field.NewEquipmentGeneralIdentificationField;
import jtk.project4.fleet.field.NewEquipmentGeneralIdentificationTabField;
import jtk.project4.fleet.field.NewEquipmentSpesificationTabField;
import jtk.project4.fleet.field.NewEquipmentTireTabField;

import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormField;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.control.TabControl;
import nl.coderight.jazz.form.control.TabItem;
import nl.coderight.jazz.form.control.TabControl.TabPlacement;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;

public class AddEditEquipmentView extends FormView{
	private FormHeader formHeader;
	private PushButton submitButton;
	private PushButton cancelButton;
	private PushButton submitButton2;
	private TabControl addNewEquipmentTab;
	private Action submitAction;
	private Action submitAction2;
	private Action cancelAction;
	private Action SaveEquipmentAction;
	
	
	@Override
	protected void init() {
		createFields();
		createLayout();	
	}	
	public void handleEvent(EquipmentEvent evt){
		switch (evt.getType()){
		case CANCEL :
			close();
			break;
			
		case SAVE :
			List<ValidationError> errors = validate();
			if(!errors.isEmpty()){
				errors.get(0).getField().focus();
				break;
			}else{
				submit();
				close();
				
			}
			default:
				propagateEvent(evt);
				break;
		}
		
	}
	
	
	private void createAction(){
		submitAction = new SubmitAction();
		submitAction2 = new SubmitAction();
		cancelAction = new CancelAction();
		//SaveEquipmentAction = new SaveEquipmentAction();
		setOnChangeAction(submitAction);
		}
	private void createFields(){
		//submitButton = new PushButton(new SaveEquipmentAction());
		//submitButton2 = new PushButton(submitAction);
		//cancelButton = new PushButton(new CancelEquipmentAction());
		addNewEquipmentTab= new TabControl();
	}
	
	private void createLayout() {
		setHeader(createHeader());
		addField(createForm());
	}
	
	private FormHeader createHeader() {
		formHeader = new FormHeader();
		formHeader.setTitle("equipment.header.form");
		return formHeader;
	}
	
	private GroupControl createForm() {
		GroupControl form = new GroupControl();
		//form.addField(new EquipmentTiresTiresConfigurationField("tirecgf"),NORTH);
		form.addField(new EquipmentTiresAxlesField("tirecfg"), NORTH);
		form.addField(createButtons(), SOUTH);		
		return form;
	}
	
	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(submitButton);
		buttons.addField(cancelButton);
		return buttons;
	}
	
	private FormField createTabs()
	{
		TabControl form = addNewEquipmentTab;
		form.setTabPlacement(TabPlacement.RIGHT);
		form.addTab(new TabItem("Identification"), new NewEquipmentGeneralIdentificationTabField("Identification"));
		form.addTab(new TabItem("Specification"), new NewEquipmentSpesificationTabField("Specification"));
		form.addTab(new TabItem("Tire"), new NewEquipmentTireTabField("tirecfg"));
		form.addTab(new TabItem("Insurance"), new EquipmentInsurancePolicyInformationField("Insurance"));
		form.addTab(new TabItem("photo"), new EquipmentPhotoPhotoField("photo"));
		form.addTab(new TabItem("notes"), new EquipmentNotesField("notes"));
		form.addTab(new TabItem("Configure"), new NewEquipmentCofigureTabField("Configure"));
		return form;
	}
	
	

}
